package com.xinjiang.electricpower.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.utils.uuid.UUID;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO;
import com.xinjiang.electricpower.domain.vo.SystemOrdersVO;
import com.xinjiang.electricpower.domain.vo.WatermarkResult;
import com.xinjiang.electricpower.domain.vo.XjOrderGeneralVO;
import com.xinjiang.electricpower.service.IXjOrderGeneralService;
import com.xinjiang.electricpower.util.WebClientUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Base64Utils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author feizhao
 * @description 新疆工单处理中心控制层类
 * @since 2022/10/31
 */
@RestController
@RequestMapping("/xinjiang-power-supply-station/repair-order-center")
@Api(value = "工单中心-工单处理中心", tags = "工单中心-工单处理中心")
@Validated//有此注解，才会校验游离的参数
@Slf4j
@RequiredArgsConstructor
public class XjOrderGeneralController extends BaseController {

    private final IXjOrderGeneralService xjOrderGeneralService;
    private final WebClientUtil webClientUtil;

    /**
     * @param xjOrderGeneralDTO 工单实体类
     * @param page              分页对象
     * @return R<IPage < XjOrderGeneralVO>> 工单列表
     */
    @GetMapping("/work-order-process-center/list")
    @ApiOperation(value = "查询待处理、已完成、处理中的工单", notes = "查询待处理、已完成、处理中的工单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xjOrderGeneralDTO", value = "工单实体类", paramType = "query", dataType = "XjOrderGeneralDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO.class),
            @ApiImplicitParam(name = "page", value = "分页对象", paramType = "query", dataType = "PageDTO<XjOrderGeneralVO>", dataTypeClass = com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO.class)
    })
    public R<PageInfo<XjOrderGeneralVO>> getXjOrderGeneralProcess(XjOrderGeneralDTO xjOrderGeneralDTO, PageDTO<XjOrderGeneralVO> page) {
        PageInfo<XjOrderGeneralVO> pages = xjOrderGeneralService.selectXjOrderGeneralPage(page, xjOrderGeneralDTO);
        return R.ok(pages, "查询成功");
    }

    /**
     * @return R<Long> 工单总数
     */
    @GetMapping("/work-order-process-center/count")
    @ApiOperation(value = "查询工单总数", notes = "查询工单总数")
    public R<Map<String, Long>> getOrderNum() {
        Map<String, Long> map = new HashMap<String, Long>();
        map.put("orderNums", xjOrderGeneralService.countOrderGeneral());
        return R.ok(map, "查询成功");
    }

    /**
     * @return R<Long> 待办工单总数
     */
    @GetMapping("/work-order-process-center/to-do-count")
    @ApiOperation(value = "查询待办工单总数", notes = "查询待办工单总数")
    public R<Map<String, Long>> getToDoNum() {
        Map<String, Long> map = new HashMap<String, Long>();
        map.put("nums", xjOrderGeneralService.countToDoOrder());
        return R.ok(map, "查询成功");
    }

    /**
     * 工单处理中心左侧菜单栏统计
     * @return
     */
    @GetMapping("/work-order-process-center/count-system-orders")
    @ApiOperation(value = "查询各个系统下的工单总数", notes = "查询各个系统下的工单总数")
    public R<List<SystemOrdersVO>> getSystemOrderNum() {
        return R.ok(xjOrderGeneralService.countSystemOrders(), "查询成功");
    }

    /**
     * 导出待处理、已完成、处理中的工单
     */
    @ApiOperation("导出待处理、已完成、处理中的工单")
    @Log(title = "导出待处理、已完成、处理中的工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xjOrderGeneralDTO", value = "xjOrderGeneralDTO类的ids属性", required = true, paramType = "body", dataType = "XjOrderGeneralDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public WatermarkResult export(HttpServletResponse response, @RequestBody XjOrderGeneralDTO xjOrderGeneralDTO) throws IOException {
        List<XjOrderGeneralVO> list = xjOrderGeneralService.selectXjOrderGenerals(xjOrderGeneralDTO.getIds());
        ExcelUtil<XjOrderGeneralVO> util = new ExcelUtil<XjOrderGeneralVO>(XjOrderGeneralVO.class);
        util.exportExcel(response, list, "待处理、已完成、处理中的工单数据");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bytes.writeTo(response.getOutputStream());
        String base64ExlCode = Base64Utils.encodeToString(bytes.toByteArray());
        String excel = "data:application/vnd.ms-excel;base64," + base64ExlCode;
        String url = "http://25.83.2.116:8088/rest/fileDataWatermark";
        Map<String, String> parameter = new HashMap<String, String>();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        parameter.put("file", "excel");
        parameter.put("taskId", UUID.fastUUID().toString());
        parameter.put("type", "csv");
        parameter.put("username", sysUser.getUserName());
        parameter.put("userNumber", String.valueOf(sysUser.getUserId()));
        parameter.put("mobile", sysUser.getPhonenumber());
        parameter.put("departmentId", sysUser.getOrgNo());
        parameter.put("dateTime", String.valueOf(LocalDateTime.now().toInstant(ZoneOffset.ofHours(8)).toEpochMilli()));
        parameter.put("clientName", "121");
        parameter.put("clientIp", sysUser.getLoginIp());
        parameter.put("symbol", ",");
        parameter.put("fileCode", "utf8");
        parameter.put("hasHead", "true");
        parameter.put("headIndex", "2");
        parameter.put("fileType", "XLSX");
        Map<String, String> header = new HashMap<String, String>();
        //请求头
        header.put("Content-Type", "application/json");
        return webClientUtil.postForm(parameter, url, header, WatermarkResult.class);
    }

    /**
     * =
     */
    @ApiOperation("导出待处理、已完成、处理中的工单，全导出或条件查询导出")
    @Log(title = "导出待处理、已完成、处理中的工单", businessType = BusinessType.EXPORT)
    @PostMapping("/exports")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "xjOrderGeneralDTO", value = "工单DTO实体类", paramType = "body", dataType = "XjOrderGeneralDTO", dataTypeClass = com.xinjiang.electricpower.domain.DTO.XjOrderGeneralDTO.class),
            @ApiImplicitParam(name = "response", value = "响应流", dataType = "HttpServletResponse", dataTypeClass = javax.servlet.http.HttpServletResponse.class)
    })
    public void exports(HttpServletResponse response, @RequestBody XjOrderGeneralDTO xjOrderGeneralDTO) {

        List<XjOrderGeneralVO> list = xjOrderGeneralService.selectXjOrderGenerals(xjOrderGeneralDTO);
        ExcelUtil<XjOrderGeneralVO> util = new ExcelUtil<XjOrderGeneralVO>(XjOrderGeneralVO.class);
        util.exportExcel(response, list, "待处理、已完成、处理中的工单数据");
    }
}
